<?php
namespace App\Services\Qrcode\Qrcode;

use App\Models\Qrcode\Qrcode;
use App\Services\Qrcode\ValidateQrcode;
use SimpleSoftwareIO\QrCode\Facades\QrCode as QrcodeModel;
/**
 * @Author: 蒋哲
 * @Date:   2017-05-27 16:25:06
 * @Last Modified by:   ws
 * @Last Modified time: 2018-06-12 
 */
class QrcodeLogic
{
    //生成二维码
    public function createQrCode($data)
    {
        $dataFlow = QrcodeModel::format('png')->size($data['size'])->generate($data['url']);
        $data['data_flow'] = base64_encode($dataFlow);
        unset($data['size']);
        $rst = Qrcode::insertGetId($data);
        if ($rst == '') {
            return auto_failure (['status' => false],'二维码生成失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'二维码未生成！');
        }
        return auto_return(['status' => true],'二维码生成成功！');
    }

    //获取二维码
    public function gainQrCode($data)
    {
        $res = Qrcode::where([['token','=',$data['token']],['appkey','=',$data['appkey']],['type','=',$data['type']],['code_id','=',$data['code_id']]])->first();

        if ($res === null) {
            return auto_failure ($data,'二维码数据未找到！');
        } elseif ($res === false) {
            return auto_failure ($data,'数据查询出错！');
        }

        return auto_return(['url' => $res['url'], 'qrcode' => $res['data_flow']],'二维码获取成功！');
    }

   
}